const fs = require('fs');

const contents = fs.readdirSync(".");
const json = {
  Indexes:[]
};

for( var i = 0, il = contents.length; i < il; i++ ) {
  const stat = fs.statSync( contents[i] );
  if( stat.isFile() ) continue;
  const index = {
    "name":`第${contents[i].substring(2)}章`,
    "entry": contents[i],
    "md": "README.md",
    "sections":[]
  };
  const contents1 = fs.readdirSync(contents[i]);

  for( var j = 0, jl = contents1.length; j < jl; j++ ) {
    const stat1 = fs.statSync( `${contents[i]}/${contents1[j]}` );
    if( stat1.isFile() ) continue;
    index.sections.push({
      "name":`第${contents1[j].substring(2)}节`,
      "entry": contents1[j],
      "md":"README.md"
    })
  }

  json.Indexes.push(index);
}

fs.writeFileSync('index.json', JSON.stringify(json, null, 2));